package com.example.njut;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;

import android.hardware.Camera;
import android.os.Build;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class FlashLightActivity extends AppCompatActivity {

    TextView showInfo;
    ImageButton flashLightBtn;
    boolean isflag = false;
    RelativeLayout flashbg;

    Camera cc;
    Camera.Parameters pp;
    SurfaceView sv;
    SurfaceHolder sh;
    //声明数组，用来保存所有需要动态开启的权限
    private static String[] PERMISSIONS_STORGE = {
            Manifest.permission.CAMERA
    };
    //权限的请求编码
    private static int REQUEST_PERMISSION_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash_light);



        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
        }
        showInfo = (TextView) findViewById(R.id.switch_text);
        flashLightBtn = (ImageButton)findViewById(R.id.switch_button);
        flashbg = (RelativeLayout)findViewById(R.id.flash_bg);
        sv = (SurfaceView)findViewById(R.id.surfaceView);
        cc = Camera.open();
        pp = cc.getParameters();
        sh = sv.getHolder();
        sh.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    cc.setPreviewDisplay(sh);
                } catch (IOException e) {
                    e.printStackTrace();
                    cc.release();
                }
                cc.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });
        flashLightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    if(isflag==false){
                        pp.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        cc.setParameters(pp);
                        showInfo.setText("点击关闭");
                        showInfo.setTextColor(Color.BLACK);
                        flashbg.setBackgroundResource(R.drawable.light);
                        flashLightBtn.setImageDrawable(getResources().getDrawable(R.drawable.on));
                        isflag = true;
                    }else{
                        pp.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        cc.setParameters(pp);
                        showInfo.setText("点击打开");
                        showInfo.setTextColor(Color.WHITE);
                        flashbg.setBackgroundResource(R.drawable.dark);
                        flashLightBtn.setImageDrawable(getResources().getDrawable(R.drawable.off));
                        isflag = false;
                    }
                }catch(Exception e){
                    e.printStackTrace();
                    Toast.makeText(FlashLightActivity.this,"发生未知错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
